package com.project.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Array;
import java.util.Arrays;

@Controller
@RequestMapping("cookie")
public class CookieController {

    @RequestMapping("createCookie")
    public String createCookie(HttpServletResponse resp,
                               HttpServletRequest req){

        //创建cookie对象
        Cookie cookie = new Cookie("name","tom");

        resp.addCookie(cookie);

        Cookie cookie1 = new Cookie("age","23");

        resp.addCookie(cookie1);
        //60秒
        cookie1.setMaxAge(60);

        //得到客户端发送给服务器的Cookie信息
        Cookie[] cookieArray = req.getCookies();

        System.out.println(Arrays.toString(cookieArray));

        for (Cookie cookie2 : cookieArray){
            System.out.println(cookie2.getName()+"  "+cookie2.getValue());
        }

        return "/html/index.html";
    }

}
